package net.dipper.tankwar.editor.ui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import net.dipper.tankwar.core.data.ObjectMap;
import net.dipper.tankwar.core.helper.ViewPort;
import net.dipper.tankwar.core.config.BackgroundItem;
import net.dipper.tankwar.core.config.ConfigManager;

import wnb.util.ui.Animation;

public class MiniMapAnimation implements Animation {
	private EditorContext context;

	public MiniMapAnimation(EditorContext context){
		this.context = context;
	}

	@Override
	public void paintFrame(Graphics2D g2, int width, int height, int frameIndex) {
		try {
			g2.clearRect(0, 0, width, height);
			g2.setPaint(Color.WHITE);
			g2.fillRect(0, 0, width, height);
			
			drawColor(g2, width, height);
			drawViewPort(g2, width, height);
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | ParserConfigurationException
				| SAXException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void drawColor(Graphics2D g2, int width, int height) throws ClassNotFoundException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException, IOException {
		ConfigManager manager = ConfigManager.getInstance();
		ObjectMap map = context.getMap();
		int mapWidth = map.getWidth();
		int mapHeight = map.getHeight();
		int gWidth = width / mapWidth;
		int gHeight = height / mapHeight;
		
		for(int i=0;i<mapWidth;i++){
			for(int j=0;j<mapHeight;j++){
				byte value = map.getBackground(i, j);
				BackgroundItem item = (BackgroundItem) manager.getBackground(value);
				g2.setPaint(item.getColor());
				g2.fillRect(i * gWidth, j * gHeight, gWidth, gHeight);
			}
		}
	}

	private void drawViewPort(Graphics2D g2, int width, int height) {
		ObjectMap map = context.getMap();
		int mapWidth = map.getWidth();
		int mapHeight = map.getHeight();
		ViewPort v = context.getViewPort();
		int gWidth = width / mapWidth;
		int gHeight = height / mapHeight;
		g2.setColor(Color.WHITE);
		g2.drawRect(v.x * gWidth, v.y * gHeight, v.width * gWidth, v.height * gHeight);
	}

	@Override
	public int getFrameCount() {
		return 0;
	}

}
